package lt.marsrutai.mobile.android.manager;

import java.util.HashMap;
import java.util.Map;

import lt.marsrutai.mobile.android.R;
import lt.marsrutai.mobile.android.util.PreferenceUtils;
import android.content.Context;

/**
 * Stores all application preference default value resolvers
 *
 * @author Justas
 *
 */
public class PreferenceDefaultValueResolvers {

    /**
     * Preferences that have default values should have their {@link DefaultValueResolver}s registered here,
     * so that their default values could be accessed in a simple way.
     */
    private static Map<String, DefaultValueResolver> defaultValueResolvers = new HashMap<String, DefaultValueResolver>();

    static {
        addDefaultValueResolver(StopSearchPreferenceManager.KEY_NEARBY_STOP_RADIUS, new DefaultValueResolver() {

            @SuppressWarnings("unchecked")
            @Override
            public <T> T resolveValue(Context context) {
                return (T) (Integer) context.getResources().getInteger(R.integer.pref_stop_search_nearby_stop_radius_default);
            }
        });

        addDefaultValueResolver(PreferenceUtils.SCHEDULE_ACTIVITY_OPENED, new DefaultValueResolver() {

            @SuppressWarnings("unchecked")
            @Override
            public <T> T resolveValue(Context context) {
                return (T) Boolean.FALSE;
            }
        });
        
        addDefaultValueResolver(PreferenceUtils.MAP_VIEW_TYPE_SATELLITE, new DefaultValueResolver() {

            @SuppressWarnings("unchecked")
            @Override
            public <T> T resolveValue(Context context) {
                return (T) (Boolean) context.getResources().getBoolean(R.bool.pref_map_view_type_satellite);
            }
        });
        
        addDefaultValueResolver(PreferenceUtils.SELECTED_SCOPE_FOLDER, new DefaultValueResolver() {

            @SuppressWarnings("unchecked")
            @Override
            public <T> T resolveValue(Context context) {
                return (T) context.getResources().getString(R.string.default_scope_folder);
            }
        });
    }

    /**
     * Add a default value resolver for the preference identified by it's <code>key</code>
     *
     * @param key Preference key
     * @param resolver Default value resolver
     */
    public static void addDefaultValueResolver(String key, DefaultValueResolver resolver) {
        defaultValueResolvers.put(key, resolver);
    }

    /**
     * Resolves default preference value
     *
     * @param context Context
     * @param key Preference key
     * @return Default preference value
     * @see DefaultValueResolver
     */
    public static <T> T resolveDefaultValue(Context context, String key) {
        DefaultValueResolver resolver = defaultValueResolvers.get(key);
        if (null != resolver) {
            return resolver.<T>resolveValue(context);
        }
        else {
            return null;
        }
    }

}
